package com.seari.controller;

import com.seari.bean.TaskBean;
import com.seari.bean.User;
import com.seari.pojo.*;
import com.seari.service.DevMaintenanceSerivce;
import com.seari.service.DevTaskService;
import com.seari.service.UserService;
import com.seari.utils.DevConstance;
import com.seari.utils.ParseUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

import java.util.List;

/**
 * 设备维护 设备分类
 */
@Controller
@RequestMapping("dev/maintenance")
public class DevMaintenanceController {

    @Autowired
    private DevMaintenanceSerivce devMaintenanceSerivce;
    @Autowired
    private DevTaskService devTaskService;
    @Autowired
    private UserService userService;

    /**
     * 根据等级获取专业，系统和设备类型数据
     * @return
     */
    @RequestMapping("treeData")
    @ResponseBody
    public MessageBean getTreeData(){
        MessageBean message = new MessageBean();
        List<TreeDataPojo> list = devMaintenanceSerivce.getTreeData();
        message.setData(list);
        message.setCode("0");
        return message;
    }

    /**
     * 设备维修计划
     * @param param
     * @return
     */
    @RequestMapping("planList")
    @ResponseBody
    public MessageBean getMaintenancePlanList(MaintenancePlanParam param){
        MessageBean message = new MessageBean("0",null,"成功");
        List<DevMaintenancePlanPojo> list = devMaintenanceSerivce.getMaintenancePlanList(param);
        message.setData(list);
        return message;
    }

    /**
     * 查询订单信息
     * @return
     */
    @RequestMapping("taskInfo")
    @ResponseBody
    public MessageBean getTaskByTaskNo(String taskNo){
        MessageBean message = new MessageBean("0",null,"成功");
        TaskBean task = devTaskService.findTaskByTaskNo(taskNo);
        message.setData(task);
        return message;
    }

    /**
     * 根据资材id查询资材维修记录
     * @param productId
     * @return
     */
    @RequestMapping("taskRecord")
    @ResponseBody
    public MessageBean record(String productId){
        MessageBean message = new MessageBean("0",null,"成功");
        List<TaskBean> taskList =
                devTaskService.findTaskListByProductId(productId, DevConstance.DEV_MAINTENANCE_FLAG_1);
        message.setData(taskList);
        return message;
    }

    /**
     * 新增、更新订单数据
     * @param task
     * @return
     */
    @RequestMapping("saveOrUpdateTask")
    @ResponseBody
    public MessageBean saveOrUpdateTask(HttpServletRequest request,TaskPojo task){
        MessageBean message = new MessageBean("0",null,"成功");
        task.setCreateUser(ParseUtils.getUidFromToken(request,userService));
        /*Claims claim = JwtUtil.parseJWT(token);
        String stationName = claim.get("stationName")+"";
        QueryUser qu = new QueryUser();
        qu.setStationName(stationName);
        qu.setUserName(claim.getSubject());
        List<User> userList = userService.getUserByParams(qu);
        if(userList!=null&&userList.size()>0){
            task.setCreateUser(userList.get(0).getUid()+"");
        }*/
        devTaskService.saveOrUpdateTask(task);
        return message;
    }

    /**
     * 查询所有的用户
     * @return
     */
    @RequestMapping("allUser")
    @ResponseBody
    public MessageBean findAllUser(){
        MessageBean message = new MessageBean("0",null,"成功");
        List<User> userList = devMaintenanceSerivce.findAllUser();
        message.setData(userList);
        return message;
    }
}
